package com.eleadmin.order.controller;

import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.order.entity.ContainerBill;
import com.eleadmin.order.entity.Customer;
import com.eleadmin.order.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * 车队管理
 * @author : Doman
 * @date : 2023-02-13 15:50:11
 * @version ：1.0
 */

@Api(tags = "车队管理")
@RestController
@RequestMapping("/api/order/carTeam")
public class CarteamController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @PreAuthorize("hasAuthority('order:carTeam:list')")
    @ApiOperation("分页查询车队管理")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Customer customer) throws Exception{
        return customerService.list(page, customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:carTeam:list')")
    @ApiOperation("查询全部车队管理")
    @GetMapping()
    public JsonResult list(Customer customer){
        return customerService.list(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:carTeam:list')")
    @ApiOperation("根据id查询车队管理")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return customerService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:carTeam:save')")
    @OperationLog
    @ApiOperation(value ="添加车队管理")
    @PostMapping()
    public JsonResult save(@RequestBody Customer customer) {
        try {
            return customerService.saveCarTeam(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:carTeam:exam')")
    @OperationLog
    @ApiOperation(value ="审核车队管理")
    @PostMapping("examCarTeam")
    public JsonResult examCarTeam(@RequestBody Customer customer) {
        try {
            return customerService.examCustomer(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:carTeam:update')")
    @OperationLog
    @ApiOperation(value ="修改车队管理")
    @PutMapping()
    public JsonResult update(@RequestBody Customer customer) {
        try {
            return customerService.update(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:carTeam:remove')")
    @OperationLog
    @ApiOperation(value ="删除车队管理")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return customerService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    @PreAuthorize("hasAuthority('order:customer:exportsCustomer')")
    @ApiOperation("导出车队司机拖车账单")
    @GetMapping("/exportsCarTeam")
    public void exportsCarTeam(ContainerBill containerBill, HttpServletResponse response, HttpServletRequest request) throws Exception{
        customerService.exportsCarTeam(containerBill, getLoginUser(),response, request);
    }


}
